package com.choosefine.it.mobile.equipment.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.mobile.equipment.dto.ShoppingIdentityDTO;
import com.choosefine.it.mobile.equipment.vo.EquipmentTypeVO;
import com.choosefine.it.mobile.equipment.vo.MyEquipmentAndSumVO;
import com.choosefine.it.mobile.equipment.vo.MyEquipmentDetailAndSumVO;
import com.choosefine.it.mobile.equipment.vo.MyEquipmentDetailVO;
import com.choosefine.it.mobile.equipment.vo.MyEquipmentSumVO;
import com.choosefine.it.mobile.equipment.vo.MyEquipmentVO;
import com.choosefine.it.mobile.equipment.vo.MyMaterialDeatilAndSumVO;
import com.choosefine.it.mobile.equipment.vo.MyMaterialDeatilVO;
import com.choosefine.it.mobile.equipment.vo.MyMaterialSpecificationsDeatilVO;
import com.choosefine.it.mobile.equipment.vo.MyMaterialSumVO;
import com.choosefine.it.mobile.equipment.vo.MyMaterialVO;
import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.HttpServiceBean;
import com.choosefine.it.base.dto.DateDTO;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(value="APP-物料-自有",description="董自齐")
@RequestMapping("/app/myEquipment")
public class APPMyEquipmentController extends BaseController{
	
	private final String dataKey="data";
	private final String dateFomart="yyyy年MM月";
	
	@Value("#{yaml.get('myEquipment.myEquipmentTypeList')}")
	private String myEquipmentTypeList;
	@Value("#{yaml.get('myEquipment.countMyEquipment')}")
	private String countMyEquipment;
	@Value("#{yaml.get('myEquipment.specifications')}")
	private String specifications;
	@Value("#{yaml.get('myEquipment.myEquipmentInfo')}")
	private String myEquipmentInfo;
	@Value("#{yaml.get('myEquipment.myMaterialTypeDetailList')}")
	private String myMaterialTypeDetailList;
	@Value("#{yaml.get('myEquipment.getEquipmentList')}")
	private String getEquipmentListUrl;
	
	@Autowired
	CustomRestTemplate restTemplate;
	
	@Autowired
	ObjectMapper jsonObjectMapper;
	
	@Autowired
	private HttpServiceBean httpServiceBean;
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value="/myEquipmentTypeList")
	@ApiOperation(value="查看自有:设施，设备",response=MyEquipmentAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="type",value="设备类型:2设施，3设备",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="equipmentId",value="种类id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",required=false,dataType="Integer",paramType="query"),
	})
	public String myEquipmentTypeList(
			Integer equipmentId,
			Integer type,
			String date,
			String projectCode,
			String userCode,
			Integer teamId,
			Integer pageNum,
			Integer pageSize
			) throws Exception{
		if(type==1){
			return responseFail("本接口不提供材料集合");
		}
		
		Map<String , Object> map=new HashMap<String, Object>();
		if(projectCode!=null){
			map.put("projectCode", projectCode);
		}
		if(teamId!=null){
			map.put("teamId", teamId);
		}
		if(userCode!=null){
			map.put("userCode", userCode);
		}
		DateDTO dat=DateUtils.conversionDateDto(date, dateFomart);
		map.put("startTime", dat.getStartTime());
		map.put("endTime", dat.getEndTime());
		map.put("type", type);
		map.put("equipmentType", equipmentId);
		map.put("pageNum",pageNum);
		map.put("pageSize", pageSize);
		
		StringBuffer myEquipmentUrl = URLHandlerUtils.getUrlBuffer(shixiaobaoURL, myEquipmentTypeList);
		httpServiceBean.urlFormat(myEquipmentUrl, map);
		
		String equipmentResult=httpServiceBean.getRequestBody(myEquipmentUrl.toString(), null);
		String listJson=httpServiceBean.resultJsonFormat(equipmentResult, dataKey).toString();
		if("".equals(listJson)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(listJson, List.class);
		
		List<MyEquipmentVO> list=new ArrayList<MyEquipmentVO>();
		MyEquipmentAndSumVO result=new MyEquipmentAndSumVO();
		if(gsList!=null){
		for(Map nows:gsList){
			MyEquipmentVO now=gs.fromJson(nows.toString(), MyEquipmentVO.class);
			list.add(now);
		}
		result.setList(list);
		/*Integer tSum=0;
		Integer mSum=0;
		result.setSum(0);
		for(int i=0;i<list.size();i++){
			tSum=list.get(i).getDayNum();
			mSum=tSum+mSum;
		}
		result.setSum(mSum);*/
		StringBuffer countMyEquipmentUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL, countMyEquipment);//购买商品总额
		map.remove("pageNum");
		map.remove("pageSize");
		httpServiceBean.urlFormat(countMyEquipmentUrl, map);//生成url
		String materielCountResult=httpServiceBean.getRequestBody(countMyEquipmentUrl.toString(), null);//查找结果
		String gsString=httpServiceBean.numberResultJsonFormat(materielCountResult,dataKey).toString();
		MyEquipmentSumVO sum=gs.fromJson(gsString, MyEquipmentSumVO.class);
		result.setSum(sum.getDays());
		return responseSuccess(result);	
		}else{
		return responseFail("暂无数据");
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value="/myMaterialTypeList")
	@ApiOperation(value="查看自有材料",response=MyMaterialVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="equipmentId",value="种类id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",required=false,dataType="Integer",paramType="query"),
	})
	public String myMaterialTypeList(
			Integer equipmentId,
			String date,
			String projectCode,
			String userCode,
			Integer teamId,
			Integer pageNum,
			Integer pageSize
			) throws Exception{
		Map<String , Object> map=new HashMap<String, Object>();
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		if(projectCode!=null){
			map.put("projectCode", projectCode);
		}
		if(teamId!=null){
			map.put("teamId", teamId);
		}
		if(userCode!=null){
			map.put("userCode", userCode);
		}

		DateDTO dat=DateUtils.conversionDateDto(date, dateFomart);	
		map.put("startTime", dat.getStartTime());
		map.put("endTime", dat.getEndTime());
		map.put("type", 1);
		map.put("equipmentType", equipmentId);
		map.put("pageNum",pageNum);
		map.put("pageSize", pageSize);
		StringBuffer myEquipmentUrl= URLHandlerUtils.getUrlBuffer(shixiaobaoURL, myEquipmentTypeList);
		httpServiceBean.urlFormat(myEquipmentUrl, map);
		String equipmentResult=httpServiceBean.getRequestBody(myEquipmentUrl.toString(), null);
		String listJson=httpServiceBean.resultJsonFormat(equipmentResult, dataKey).toString();
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(listJson, List.class);
		
		List<MyMaterialVO> list=new ArrayList<MyMaterialVO>();
		if(gsList!=null){
		for(Map nows:gsList){
			MyMaterialVO now=gs.fromJson(nows.toString(), MyMaterialVO.class);
			now.setTotalNum(now.getNum()+now.getAdjustNumber());
			list.add(now);
		}
		return responseSuccess(list);
		}else{
		return responseFail("暂无数据");	
		}
		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value="/myMaterialTypeDetail")
	@ApiOperation(value="查看自有材料详情",response=MyMaterialDeatilAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="equipmentType",value="种类id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="date",value="时间",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",required=false,dataType="Integer",paramType="query"),
	})
	public String myMaterialTypeDetailList(
			@RequestParam(value="equipmentType",defaultValue="")Integer equipmentType,
			@RequestParam(value="date",defaultValue="")String date,
			@RequestParam(value="isSum",defaultValue="2")Integer isSum,
			String projectCode,
			String userCode,
			Integer teamId,
			Integer pageNum,
			Integer pageSize
			) throws Exception{
		if(equipmentType==null){
			return responseFail("需要品类");
		}
		Map<String , Object> map=new HashMap<String, Object>();
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		ShoppingIdentityDTO siDto=new ShoppingIdentityDTO();
		if(projectCode!=null){
			siDto.setProjectCode(projectCode);
		}
		if(teamId!=null){
			siDto.setTeamId(teamId);
		}
		if(userCode!=null){
			siDto.setUserCode(userCode);
		}
		DateDTO dat=DateUtils.conversionDateDto(date, dateFomart);
		map.put("type", 1);
		map.put("equipmentType", equipmentType);
		StringBuffer myMaterialTypeDetailUrl= URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  myMaterialTypeDetailList);
		httpServiceBean.urlFormat(myMaterialTypeDetailUrl, map, page, dat,siDto);
		String myMaterialTypeDetailResult=httpServiceBean.getRequestBody(myMaterialTypeDetailUrl.toString(), null);
		String listJson=httpServiceBean.resultJsonFormat(myMaterialTypeDetailResult, dataKey).toString();
		if("".equals(listJson)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		MyMaterialDeatilAndSumVO result=new MyMaterialDeatilAndSumVO();
		List<Map> gsList=gs.fromJson(listJson, List.class);
		List<MyMaterialDeatilVO> list=new ArrayList<MyMaterialDeatilVO>();
		for(Map nows:gsList){
			MyMaterialDeatilVO now=gs.fromJson(nows.toString(), MyMaterialDeatilVO.class);
			list.add(now);
		}
		result.setList(list);
		/*Integer tSum=0;
		Integer mSum=0;
		result.setSum(0);
		for(int i=0;i<list.size();i++){
			tSum=list.get(i).getSumNumber();
			mSum=tSum+mSum;
		}
		result.setSum(mSum);*/
		if(isSum==1){
			
			StringBuffer sumUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  countMyEquipment);
			httpServiceBean.urlFormat(sumUrl, map);
			String jsonSum=httpServiceBean.getRequestBody(sumUrl.toString(), null);
			String sumResult=httpServiceBean.resultJsonFormat(jsonSum, dataKey).toString();
			MyMaterialSumVO sum=gs.fromJson(sumResult, MyMaterialSumVO.class);
			result.setSum(sum.getSum());
		}
		return responseSuccess(result);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value="/specificationsDetailList")
	@ApiOperation(value="查看自有材料详情二级列表",response=MyMaterialSpecificationsDeatilVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="equipmentType",value="种类id",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="specifications",value="规格名",required=true,dataType="String",paramType="query"),
		@ApiImplicitParam(name="date",value="时间",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",required=false,dataType="Integer",paramType="query"),
	})
	public String specificationsDetailList(
			@RequestParam(value="equipmentType",defaultValue="")Integer equipmentType,
			@RequestParam(value="specifications",defaultValue="")String specifications,
			@RequestParam(value="date",defaultValue="")String date,
			String projectCode,
			String userCode,
			Integer teamId,
			Integer pageNum,
			Integer pageSize
			) throws Exception{
		if(equipmentType==null||specifications==null){
			return responseFail("缺少品类或规格");
		}
		Map<String , Object> map=new HashMap<String, Object>();
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		ShoppingIdentityDTO siDto=new ShoppingIdentityDTO();
		if(projectCode!=null){
			siDto.setProjectCode(projectCode);
		}
		if(teamId!=null){
			siDto.setTeamId(teamId);
		}
		if(userCode!=null){
			siDto.setUserCode(userCode);
		}
		DateDTO dat=DateUtils.conversionDateDto(date, dateFomart);
		map.put("type", 1);
		map.put("equipmentType", equipmentType);
		map.put("specifications", specifications);
		
		StringBuffer specificationsDetailUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  specifications);
		httpServiceBean.urlFormat(specificationsDetailUrl, map, page, dat,siDto);
		
		String specificationsDetailResult=httpServiceBean.getRequestBody(specificationsDetailUrl.toString(), null);
		String listJson=httpServiceBean.resultJsonFormat(specificationsDetailResult, dataKey).toString();
		if("".equals(listJson)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(listJson, List.class);
		List<MyMaterialSpecificationsDeatilVO> result=new ArrayList<MyMaterialSpecificationsDeatilVO>();
		for(Map nows:gsList){
			MyMaterialSpecificationsDeatilVO now=gs.fromJson(nows.toString(), MyMaterialSpecificationsDeatilVO.class);
			result.add(now);
		}
		return responseSuccess(result);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value="/myEquipmentTypeDetailList")
	@ApiOperation(value="查看自有设备，设施详情列表",response=MyEquipmentDetailAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="type",value="设备类型:2设施，3设备",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="equipmentType",value="种类Id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="isGoBack",value="是否离场：1全部，2未离场，3以离场",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="date",value="时间",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",required=false,dataType="Integer",paramType="query"),
	})
	public String myEquipmentTypeDetailList(
			@RequestParam(value="type",defaultValue="")Integer type,
			@RequestParam(value="isSum",defaultValue="2")Integer isSum,
			@RequestParam(value="equipmentType",defaultValue="2")Integer equipmentType,
			@RequestParam(value="date",defaultValue="")String date,
			@RequestParam(value="isGoBack",defaultValue="1")Integer isGoBack,
			String projectCode,
			String userCode,
			Integer teamId,
			Integer pageNum,
			Integer pageSize
			) throws Exception{
		Map<String , Object> map=new HashMap<String, Object>();
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		ShoppingIdentityDTO siDto=new ShoppingIdentityDTO();
		if(projectCode!=null){
			siDto.setProjectCode(projectCode);
		}
		if(teamId!=null){
			siDto.setTeamId(teamId);
		}
		if(userCode!=null){
			siDto.setUserCode(userCode);
		}
		DateDTO dat=DateUtils.conversionDateDto(date, dateFomart);
		map.put("type", type);
		map.put("equipmentType", equipmentType);
		map.put("isGoBack", isGoBack);
		
		StringBuffer myEquipmentTypeDetailUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  myEquipmentInfo);
		httpServiceBean.urlFormat(myEquipmentTypeDetailUrl, map, page, dat,siDto);
		
		String myEquipmentTypeDetailResult=httpServiceBean.getRequestBody(myEquipmentTypeDetailUrl.toString(), null);
		String listJson=httpServiceBean.resultJsonFormat(myEquipmentTypeDetailResult, dataKey).toString();
		if("".equals(listJson)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		MyEquipmentDetailAndSumVO result=new MyEquipmentDetailAndSumVO();
		List<Map> gsList=gs.fromJson(listJson, List.class);
		List<MyEquipmentDetailVO> list=new ArrayList<MyEquipmentDetailVO>();
		for(Map nows:gsList){
			MyEquipmentDetailVO now=gs.fromJson(nows.toString(), MyEquipmentDetailVO.class);
			list.add(now);
		}
		result.setList(list);
		/*Integer tSum=0;
		Integer mSum=0;
		result.setSum(0);
		for(int i=0;i<list.size();i++){
			tSum=list.get(i).getDayNum();
			mSum=tSum+mSum;
		}
		result.setSum(mSum);*/
		
		StringBuffer sumUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  countMyEquipment);
		httpServiceBean.urlFormat(sumUrl, map);
		
		String jsonSum=httpServiceBean.getRequestBody(sumUrl.toString(), null);
		String sumString=httpServiceBean.resultJsonFormat(jsonSum, dataKey).toString();
		MyEquipmentSumVO sum=gs.fromJson(sumString, MyEquipmentSumVO.class);
		result.setSum(sum.getDays());
		
		return responseSuccess(result);
	}
	@RequestMapping(value="/getEquipmentList",method=RequestMethod.GET)
	@ApiOperation(value="查询自有材料的查询列表",response=EquipmentTypeVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组id",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户code",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="type",value="1材料2设备3设施",required=true,dataType="Integer",paramType="query"),
	})
	public String getEquipmentList(@RequestParam(value="projectCode",defaultValue="") String projectCode,
			@RequestParam(value="teamId",defaultValue="") Integer teamId,
			@RequestParam(value="userCode",defaultValue="") String userCode,
			Integer type){
		StringBuffer urls= URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  getEquipmentListUrl);
		if (projectCode != null) {
			urls.append("projectCode=" + projectCode + "&");
		}
		if (teamId != null) {

			urls.append("teamId=" + teamId + "&");
		}
		if (userCode != null) {

			urls.append("userCode=" + userCode + "&");
		}
		if (type != null) {

			urls.append("type=" + type + "&");
		}
		String url = URLHandlerUtils.DefaultUriTemplateHandler(urls.toString());
		Map map = restTemplate.getForObject(url, Map.class, new HashMap<>());
		List tfList = (List) map.get("data");
		List<EquipmentTypeVO> elist=new ArrayList<>();
		EquipmentTypeVO etVo=null;
		for(int i=0;i<tfList.size();i++){
			etVo=new EquipmentTypeVO();
			Map equipmentType =(Map) tfList.get(i);
			etVo.setEquipemntTypeName((String)equipmentType.get("equipemntTypeName"));
			etVo.setEquipmentType((Integer)equipmentType.get("equipmentType"));
			elist.add(etVo); 
		}
		return responseSuccess(elist);
	}
}
